package cn.edu.xmu.oomall.customer.service;

import cn.edu.xmu.javaee.core.aop.LoginUser;
import cn.edu.xmu.javaee.core.exception.BusinessException;
import cn.edu.xmu.javaee.core.model.ReturnNo;
import cn.edu.xmu.javaee.core.model.ReturnObject;
import cn.edu.xmu.javaee.core.model.dto.UserDto;
import cn.edu.xmu.javaee.core.util.JwtHelper;
import cn.edu.xmu.oomall.customer.controller.dto.CustomerDto;
import cn.edu.xmu.oomall.customer.controller.dto.UserQueryDto;
import cn.edu.xmu.oomall.customer.dao.CustomerDao;
import cn.edu.xmu.oomall.customer.dao.bo.Customer;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * @author 谢传艳
 * @date 2024/12/18
 */
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.REQUIRED)
public class CustomerService {
    private final CustomerDao customerDao;

    /**
     * 管理员查询所有用户信息
     * @param userQueryDto 用户查询信息
     * @param page 页码
     * @param pageSize 每页大小
     * @return List<Customer>
     */
    public  List<Customer> getAllUserInfo(UserQueryDto userQueryDto, Integer page, Integer pageSize) {
        return customerDao.retrieveByUserNameAndNameAndMobile(userQueryDto, page, pageSize);
    }

    /**
     * 用户注册
     * @param customer 用户信息
     * @return Customer
     */
    public Customer registerUser(Customer customer) {
        Customer customerByUserName = customerDao.retrieveByUserName(customer.getUserName());
        if(Objects.nonNull(customerByUserName)){
            throw new BusinessException(ReturnNo.CUSTOMER_NAMEEXIST,ReturnNo.CUSTOMER_NAMEEXIST.getMessage());
        }
        Customer customerByMobile = customerDao.retrieveByMobile(customer.getMobile());
        if(Objects.nonNull(customerByMobile)){
            throw new BusinessException(ReturnNo.CUSTOMER_MOBILEEXIST,ReturnNo.CUSTOMER_MOBILEEXIST.getMessage());
        }
        customer.setGmtCreate(LocalDateTime.now());
        customer.setInvalid(Customer.VALID);
        customer.setPoint(Customer.NO_POINT);
        customer.setCreatorId(Customer.DEFAULT_CREATOR_ID);
        customer.setCreatorName(Customer.DEFAULT_CREATOR_NAME);
        return customerDao.insert(customer);
    }

    /**
     * 获取用户信息
     * @param id 用户id
     * @return Customer
     */
    public Customer getSelfInfo(Long id) {
        return customerDao.findById(id);
    }


    /**
     * 修改用户信息
     * @param customer 用户信息
     * @return Customer
     */
    public void modifiedMyselfInfo(Customer customer, UserDto user) {
        Customer oldCustomer = customerDao.findById(customer.getId());
        oldCustomer.modifiedMyselfInfo(customer, user);
    }

    /**
     * 用户登录
     * @param customer 用户信息
     * @return Customer
     */
    public String login(Customer customer) {
        Customer customerByUserName = customerDao.retrieveByUserName(customer.getUserName());
        if(Objects.isNull(customerByUserName)){
            throw new BusinessException(ReturnNo.CUSTOMER_INVALID_ACCOUNT,ReturnNo.CUSTOMER_INVALID_ACCOUNT.getMessage());
        }
        if(!customerByUserName.getPassword().equals(customer.getPassword())){
            throw new BusinessException(ReturnNo.CUSTOMER_INVALID_ACCOUNT,ReturnNo.CUSTOMER_INVALID_ACCOUNT.getMessage());
        }
        JwtHelper jwtHelper = new JwtHelper();
        return jwtHelper.createToken(customerByUserName.getId(), customerByUserName.getUserName(), Customer.DEFAULT_DEPART_ID, Customer.DEFAULT_USER_LEVEL,  Customer.DEFAULT_EXPIRE_TIME);

    }

    /**
     * 管理员根据id查询所有用户信息
     * @param id 用户id
     * @return Customer
     */
    public Customer getUserById(Long id) {
        return customerDao.findById(id);
    }

    /**
     * 管理员删除用户
     * @param id 用户id
     * @param user 用户信息
     */
    public void delUserById(Long id, UserDto user) {
        Customer customer = customerDao.findById(id);
        customer.delete(user);
    }

    /**
     * 管理员封禁用户
     * @param id 用户id
     * @param user 用户信息
     */
    public void banUser(Long id, UserDto user) {
        Customer customer = customerDao.findById(id);
        customer.ban(user);
    }

    /**
     * 管理员解禁用户
     * @param id 用户id
     * @param user 用户信息
     */
    public void releaseUser(Long id, UserDto user) {
        Customer customer = customerDao.findById(id);
        customer.release(user);
    }
}
